import { Action,Db } from 'uokay'
import Message from '../b/Message'
import database from '../database'
export default class ConversationAction extends Action {

    send(okay, event, pipe) {
        let msg = okay.data.msg
        let conv_id = okay.data.conversation
        let friend_id = okay.data.$route.userid
        if (msg == '') {
            return
        }
        let message = new Message('primary', '', { friend: friend_id, msg: msg })
        pipe.set('message', message)
        let db = Db.i(database)
        let et_message = {
            convid: conv_id,
            sender: sessionStorage.getItem('id'),
            receiver: friend_id,
            message: msg,
            createtime: new Date().getTime()
        }
        return db.table('et_message').insert(et_message)
            .then(result => {
                et_message.nickname = okay.actions.user.user.nickname
                et_message.avatar = okay.actions.user.user.avatar
                okay.data.messages.push(et_message)
                return true
            }).catch(error => {
                console.log(error)
                return Action.EOA
        })
    }

}